package rsj.sql.contract;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import rsj.annotation.DynamicSelect;
import rsj.annotation.Param;
import rsj.annotation.Select;
import rsj.annotation.Update;
import rsj.api.contract.entity.Contract;
import rsj.service.contract.dynamic.ContractDynamic;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 合同
 *
 * @author yuanqinglong
 * @since 2021/7/16 17:04
 */
public interface ContractDao {


    /**
     * 合同列表（不包含 已申请和申请已处理的合同）
     *
     * @param orgId    机构id
     * @param pageSize 页面大小
     * @param pageNow  页面现在
     * @return {@link TableNode}
     */
    @Select(value = "SELECT orgp_id,\n" +
            "       orgp_prod_uid,\n" +
            "       orgp_prod_name,\n" +
            "       orgp_type,\n" +
            "       orgp_status,\n" +
            "       orgp_billmethod,\n" +
            "       orgp_buyamount,\n" +
            "       orgp_useamount,\n" +
            "       orgp_ctime,\n" +
            "       orgp_endtime,\n" +
            "       orgp_utime AS orgp_contractime,\n" +
            "       orgp_operson,\n" +
            "       orgp_rechargetime,\n" +
            "       orgp_orgpstatus,\n" +
            "       orgp_prod_charge,\n" +
            "       COALESCE(CAST(NULLIF(orgp_bus_id,'') AS VARCHAR),'-')  AS orgp_bus_id FROM sys.sys_orgp WHERE orgp_org_id = #{orgId} AND orgp_status <> '40' AND orgp_status <> '70' ORDER BY orgp_utime DESC  OFFSET #{pageNow} LIMIT #{pageSize} ")
    TableNode contractList(@Param("orgId") String orgId, @Param("pageSize") Integer pageSize, @Param("pageNow") Integer pageNow);


    /**
     * 合同数
     *
     * @param orgId 机构id
     * @return {@link TableNode}
     */
    @Select(value = "SELECT COUNT(*) FROM sys.sys_orgp WHERE orgp_org_id = #{orgId} AND orgp_status <> '40' AND orgp_status <> '70' ")
    Node contractCount(@Param("orgId") String orgId);


    /**
     * 添加合同
     *
     * @param contract 合同
     */
    @Update("INSERT INTO sys.sys_orgp (orgp_id,orgp_org_id,orgp_prod_uid,orgp_prod_name,orgp_billmethod,orgp_buyamount,orgp_ctime,orgp_endtime," +
            "orgp_operson,orgp_bus_id,orgp_contractime,orgp_prod_charge,orgp_status,orgp_type,orgp_orgpstatus,orgp_prod_id,orgp_utime,orgp_rechargetime) VALUES (#{orgpId},#{orgpOrgId}," +
            "#{orgpProdUid},#{orgpProdName},#{orgpBillmethod},#{orgpBuyamount},#{orgpCtime},#{orgpEndtime},#{orgpOperson},#{orgpBusId},#{orgpContractime}," +
            "#{orgpProdCharge},#{orgpStatus},#{orgpType},#{orgpOrgpstatus},#{orgpProdId},#{orgpUtime},#{orgpRechargetime})")
    void addContract(Contract contract);


    /**
     * 检查合同中产品的使用
     *
     * @param orgId   org id
     * @param prodUid 促使uid
     * @return {@link Node}
     */
    @Select(value = "SELECT orgp_id FROM sys.sys_orgp WHERE orgp_org_id = #{orgId} AND orgp_prod_uid LIKE #{%prodUid%} AND orgp_status IN ('10','20','55')")
    TableNode checkContractedProduct(@Param("orgId") String orgId, @Param("prodUid") String prodUid);


    /**
     * 更新合同状态
     *
     * @param orgpId 合同id
     * @param status 合同状态（0停用、1启用）
     */
    @Update(value = "UPDATE sys.sys_orgp SET orgp_orgpstatus = #{status} WHERE orgp_id = #{orgpId}")
    void updateContractStatus(@Param("orgpId") String orgpId, @Param("status") String status);

    /**
     * 机构下的合同
     *
     * @param orgId org id
     * @return {@link TableNode}
     */
    @Select(value = "SELECT orgp_id FROM sys.sys_orgp WHERE orgp_org_id = #{orgId}")
    TableNode contractUnderOrganization(@Param("orgId") String orgId);


    /**
     * 包量合同
     *
     * @param orgIds 机构id
     * @return {@link TableNode}
     */
    @DynamicSelect(type = ContractDynamic.class, method = "combinationContract")
    TableNode combinationContract(@Param("orgIds") List<String> orgIds);


    /**
     * 检查合同可以创建
     *
     * @param orgId       org id
     * @param prodId      促使id
     * @param contractime contractime
     * @return {@link TableNode}
     */
    @Select(value = "SELECT count(1) FROM sys.sys_orgp WHERE orgp_org_id = #{orgId} AND orgp_prod_id = #{prodId} AND orgp_contractime = #{contractime}")
    Node checkContractCanCreate(@Param("orgId") String orgId, @Param("prodId") String prodId, @Param("contractime") LocalDate contractime);


    /**
     * 申请状态改为已申请
     *
     * @param orgId      机构id
     * @param orgpStatus 合同状态
     * @param prodId     接口id
     * @param orgpUtime  更新时间
     */
    @Select(value = "UPDATE sys.sys_orgp SET orgp_status = #{orgpStatus},orgp_utime = #{orgpUtime} WHERE orgp_status = '40' AND orgp_org_id = #{orgId} AND orgp_prod_id = #{prodId}")
    void changeAppliedStatus(@Param("orgId") String orgId, @Param("orgpStatus") String orgpStatus, @Param("prodId") String prodId, @Param("orgpUtime") LocalDateTime orgpUtime);
}
